package action;

import Impl.TopicDaoImpl;
import Impl.plateDaoImpl;
import dao.TopicDao;
import entity.Plate;
import entity.page.ListPage;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

//@WebServlet(name = "ShowListPageAction", urlPatterns = "/showlist.jhtml")
public class ShowListPageAction extends HttpServlet {
    TopicDao topicDao = new TopicDaoImpl();
    plateDaoImpl sectionDao = new plateDaoImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("----------正在执行showlistpageAction-----------");
        Integer sid = Integer.parseInt(request.getParameter("sid"));
        ArrayList<ListPage> AlllistPages = topicDao.getTopicInfobyid(sid);
        ArrayList<ListPage> listPages = new ArrayList<>();
        ArrayList<Plate> listSections = (ArrayList<Plate>) sectionDao.findPlateBySid(sid);

        request.getSession().setAttribute("sid", sid);
        boolean isFather = false;
        if (listSections.size() > 0) {
            // 是父版块，显示版块的信息listSections
            request.setAttribute("listSections", listSections);
            isFather = true;
            request.setAttribute("isFather", isFather);
            request.getRequestDispatcher("/user/topic_list.jsp").forward(request, response);
            System.out.println("ShowListPageAction.java: Show father block message");


        } else {
            isFather = false;
            request.setAttribute("isFather", isFather);
            System.out.println("ShowListPageAction.java: Show Topic message");

            // 用于帖子的翻页
            int pageNow = 0;
            if(request.getParameter("pageNow") == null) {
                pageNow = 1;
            }else {
                pageNow = Integer.parseInt(request.getParameter("pageNow"));
            }
            listPages.clear();
            int allCount = AlllistPages.size();
            int allPage;
            if (allCount % 10 ==0) {
                allPage = allCount / 10;
            } else {
                allPage = allCount / 10 + 1;
            }

            for (int i = (pageNow - 1) * 10; i < pageNow * 10 && i < AlllistPages.size(); i++) {
                listPages.add(AlllistPages.get(i));
            }
//            request.setAttribute("sid", sid);
            request.setAttribute("allpage", allPage);
            request.setAttribute("pageNow", pageNow);
            request.setAttribute("listPage", listPages);
            request.getRequestDispatcher("#").forward(request, response);

            // 是子版块，显示帖子的信息listPages
//            if (listPages.size() > 0) {
//                request.getRequestDispatcher("list.jsp").forward(request, response);
//            }

        }



    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
